﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALHelper.Business;
using System.Data.SqlClient;
using System.Data;

namespace DALHelper.DAL
{
    public class UserDAL
    {
        SqlConnection m_connection;

        public UserDAL(SqlConnection conn)
        {
            m_connection = conn;
        }

        //public void AddUser(UserManager user)
        //{

        //}

        //public void RemoveUser(string sUserName)
        //{
        //    //SqlConnection conn = new SqlConnection();
        //    //conn.BeginTransaction()
        //}

        public bool Login(string sUserName, string sPassword, out long lTransID)
        {
            lTransID = 0;

            m_connection.Open();

            SqlCommand cmd = new SqlCommand("sp_DangNhap", m_connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter para;

            para = new SqlParameter("TenDangNhap", System.Data.SqlDbType.NVarChar);
            para.Direction = ParameterDirection.Input;
            para.Value = sUserName;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MatKhau", System.Data.SqlDbType.NVarChar);
            para.Direction = ParameterDirection.Input;
            para.Value = sPassword;
            cmd.Parameters.Add(para);

            para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
            para.Direction = ParameterDirection.Input;
            para.Value = 0;
            cmd.Parameters.Add(para);

            try
            {
                SqlDataReader result = cmd.ExecuteReader();
                while (result.Read())
                {
                    lTransID = long.Parse(result["MA_DOAN"].ToString().Substring(2));
                }
                
                return true;
            }
            catch (SqlException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                m_connection.Close();
            }
        }

        public void Logout(string sUserName)
        {
            m_connection.Open();

            SqlCommand cmd = new SqlCommand("sp_DangXuat", m_connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter para;

            para = new SqlParameter("TenDangNhap", System.Data.SqlDbType.NVarChar);
            para.Direction = ParameterDirection.Input;
            para.Value = sUserName;
            cmd.Parameters.Add(para);

            para = new SqlParameter("Wait", System.Data.SqlDbType.Bit);
            para.Direction = ParameterDirection.Input;
            para.Value = 0;
            cmd.Parameters.Add(para);

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                m_connection.Close();
            }
        }
    }
}
